package com.itheima.controller;

import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.entity.Orders;
import com.itheima.service.OrdersService;
import com.itheima.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单模块
 * @author LIFE
 * @date 2022-06-07 09:19:53
 **/
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @GetMapping("/userPage")
    public PageResult historicalOrder(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Map<String, Object> map = ordersService.historicalOrder(page,pageSize);
        Long pages = (Long) map.get("pages");
        List<OrderVO> orderList = (List<OrderVO>) map.get("orderVOList");
        if (orderList == null){
            orderList = new ArrayList<>();
        }
        return new PageResult(Code.OK, MessageConstant.GET_ORDER_SUCCESS,orderList,pages);
    }
    /**
     * 功能描述: 提交订单
     * @param orders
     * @return : com.itheima.common.Result
     */
    @PostMapping("/submit")
    public Result orders(@RequestBody Orders orders){
        int row = ordersService.submitOrder(orders);
        if(row<1){
            return new Result(Code.ERROR,MessageConstant.ADD_ORDER_ERROR);
        }
        return new Result(Code.OK,MessageConstant.ADD_ORDER_SUCCESS);
    }
}
